/*
 * Copyright 2008-2009 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public Licence Version 3
 * The licence can be read in its entirety in the LICENSE.txt file accompaning this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of WeightWatch.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package net.cachapa.weightwatch.ui;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListCellRenderer;
import java.util.Vector;
import net.cachapa.weightwatch.Value;

public class DatabaseListForm extends Form implements ActionListener {
	private Application application;
	private List list;
	private Command deleteCommand;
	private Command backCommand;
	
	public DatabaseListForm(Application application) {
		this.application = application;
		this.setTitle("database_title");
		this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		this.setScrollable(false);
		this.setFocusable(false);
		
		this.list = new List();
		this.list.setNumericKeyActions(false);
		this.list.setListCellRenderer(new DatabaseListCellRenderer());
		this.list.setFixedSelection(List.FIXED_NONE_CYCLIC);
		this.addComponent(this.list);
		
		deleteCommand = new Command("delete");
		backCommand = new Command("back");
		this.addCommand(deleteCommand);
		this.addCommand(backCommand);
		this.setClearCommand(deleteCommand);
		this.setBackCommand(backCommand);
		this.setCommandListener(this);
	}
	
	public void show() {
		Vector data = this.application.getWeightWatch().getFullDatabase();
		int size = data.size();
		Vector reverseData = new Vector(size);
		for (int i = size - 1; i >= 0; i--) {
			reverseData.addElement(data.elementAt(i));
		}
		this.list.setModel(new DefaultListModel(reverseData));
		super.show();
	}

	public void actionPerformed(ActionEvent ae) {
		Object source = ae.getSource();
		if (source == deleteCommand)
		{
			boolean result = Dialog.show("database_erasemessagetitle", list.getSelectedItem().toString(),
				Dialog.TYPE_CONFIRMATION ,null, "delete", "cancel");
			if (result) {
				Value value = (Value)list.getSelectedItem();
				list.getModel().removeItem(list.getSelectedIndex());
				application.getWeightWatch().deleteValue(value);
				application.performAction(Application.UPDATE_CHARTS);
			}
			if (list.size() == 0) {
				this.removeCommand(deleteCommand);
			}
		} else if (source == backCommand) {
			application.performAction(Application.SHOW_PREFERENCES_FORM);
		}
	}

	private class DatabaseListCellRenderer extends Label implements ListCellRenderer {

		public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
			setText(value.toString());
			this.setFocus(isSelected);
			return this;
		}

		public Component getListFocusComponent(List list) {
			Label label = new Label("");
			label.setFocus(true);
			label.getStyle().setBgTransparency(255);
			return label;
		}
	}
}
